package com.ruoyi.produce.process.planself.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.process.planoutsource.domain.ProducePlanOutsource;
import com.ruoyi.produce.process.planself.domain.ProducePlanMonthlyProduction;
import com.ruoyi.produce.process.planself.domain.ProducePlanSelfCond;
import com.ruoyi.produce.process.planself.domain.ProducePlanSelfDomain;
import com.ruoyi.produce.process.planself.domain.ProducePlanSelfDto;
import com.ruoyi.produce.process.planself.service.IProducePlanSelfService;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.psi.sale.order.domain.SaleOrderProduct;
import com.ruoyi.psi.sale.order.service.ISaleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自产生产计划信息
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.13
 */
@RestController
@RequestMapping("/produce/planself")
@Api(value = "生产计划信息模块", tags = "生产计划接口")
public class ProducePlanSelfController extends BaseController {

    @Autowired
    private IProducePlanSelfService producePlanSelfService;

    @Autowired
    private ISaleOrderService saleOrderService;


    /**
     * 获取成品自产生产计划
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取成品生产计划列表",notes = "查询所有成品生产计划信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProducePlanSelfCond cond){
        startPage();
        List<ProducePlanSelfDto> producePlanSelfDtos = producePlanSelfService.selectProducePlanSelfListByFinished(cond);
        return getDataTable(producePlanSelfDtos);
    }

    /**
     * 获取未完成成品自产生产计划
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取正在生产的成品生产计划列表",notes = "查询所有未完成成品生产计划信息")
    @PostMapping("/list/processing")
    public TableDataInfo selectProcessingProducePlanSelfListByFinished(@RequestBody ProducePlanSelfCond cond){
        startPage();
        List<ProducePlanSelfDto> producePlanSelfDtos = producePlanSelfService.selectProcessingProducePlanSelfListByFinished(cond);
        return getDataTable(producePlanSelfDtos);
    }

    /**
     * 导出自产生产计划
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ProducePlanSelfCond cond)
    {
        List<ProducePlanSelfDto> producePlanSelfDtos = producePlanSelfService.selectProducePlanSelfListByFinished(cond);
        ExcelUtil<ProducePlanSelfDto> util = new ExcelUtil<>(ProducePlanSelfDto.class);
        return util.exportExcel(producePlanSelfDtos, "自产生产计划数据");
    }

    /**
     * 新增自产生产计划
     * @param producePlanSelf
     * @return
     */
    @ApiOperation(value = "新增生产计划", notes = "新增生产计划信息")
    @ApiImplicitParam(name = "producePlanSelf", value = "新增生产计划(参数：planSelfId，planSelfIdParent，saleOrderId，produceId，producePlanType，planSelfNumber，endTime，remark)", dataType = "ProducePlanSelf")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProducePlanSelfDomain producePlanSelf){

        return toAjax(producePlanSelfService.insertProducePlanSelf(producePlanSelf));
    }

    /**
     * 修改自产生产计划
     * @param producePlanSelf
     * @return
     */
    @ApiOperation(value = "修改生产计划", notes = "修改生产计划")
    @ApiImplicitParam(name = "producePlanSelf", value = "修改生产计划(参数：produceId，saleOrderId，endTime，remark)", dataType = "ProducePlanSelf")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProducePlanSelfDomain producePlanSelf)
    {
        return toAjax(producePlanSelfService.updateProducePlanSelf(producePlanSelf));
    }

    /**
     * 删除自产生产计划
     * 仅修改删除标志位
     * @param producePlanSelf
     * @return
     */
    @ApiOperation(value = "删除生产计划", notes = "删除生产计划,仅删除标志位")
    @ApiImplicitParam(name = "producePlanSelf", value = "删除生产计划", dataType = "ProducePlanSelf")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProducePlanSelfDomain producePlanSelf)
    {

        return toAjax(producePlanSelfService.updateProducePlanSelfDeleteFlag(producePlanSelf));
    }

    /**
     * 批量生成生产报价
     * @param args 生产计划编号数组，且不能为空
     * @return
     */
    @ApiOperation(value = "生成一条生产报价", notes = "生成一条生产报价")
    @PostMapping("/offer/{args}")
    public AjaxResult releaseProduceOffer(@PathVariable String[] args) {
        return toAjax(producePlanSelfService.releaseProduceOffer(args));
    }

    /**
     * 下达加工单
     * @param workSheet
     * @return
     */
    @ApiOperation(value = "下达加工单", notes = "下达加工单")
    @PostMapping("/releaseWorksheet")
    public AjaxResult releaseWorksheet(@RequestBody ProduceWorkSheet workSheet){
        return toAjax(producePlanSelfService.releaseWorksheet(workSheet));
    }

    /**
     * 下达委外加工单
     * @param planOutsource
     * @return
     */
    @ApiOperation(value = "下达委外加工单", notes = "下达委外加工单")
    @PostMapping("/releaseProducePlanOutsource")
    public AjaxResult releaseProducePlanOutsource(@RequestBody ProducePlanOutsource planOutsource){
        return toAjax(producePlanSelfService.releaseProducePlanOutsource(planOutsource));
    }

    /**
     * 查询月销售产量
     * 分月
     * @return
     */
    @ApiOperation(value = "查询月销售产量（分月）",notes = "查询月销售产量（分月）")
    @GetMapping("/selectProductOrderQuantity")
    public AjaxResult selectProductOrderQuantity() {
        List<ProducePlanMonthlyProduction> producePlanMonthlyProductions = producePlanSelfService.searchMonthlyProduction();
        return AjaxResult.success("查询成功", producePlanMonthlyProductions);
    }

    /**
     * 查询销售订单进度
     * @return
     */
    @ApiOperation("查询销售订单进度")
    @GetMapping("/info")
    public AjaxResult searchSaleOrderProgress(@RequestParam String saleOrderId) {
        if (!StringUtils.isNotNull(saleOrderService.selectSaleOrderById(saleOrderId))) {
            return AjaxResult.error("该销售订单不存在，请重新输入");
        }
        return AjaxResult.success(producePlanSelfService.searchSaleOrderProgress(saleOrderId));
    }
}
